class Main extends egret.DisplayObjectContainer {

  /**
   * 加载进度界面
   * Process interface loading
   */
  private loadingView: LoadingUI;
  private startPage = new StartPage();

  public constructor() {
    super();
    this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
  }

  private onAddToStage(event: egret.Event) {
    /*egret.lifecycle.addLifecycleListener((context) => {
      // custom lifecycle plugin

      context.onUpdate = () => {

      }
    });

    egret.lifecycle.onPause = () => {
      egret.ticker.pause();
    };


    egret.lifecycle.onResume = () => {
      egret.ticker.resume();
    };*/


    //设置加载进度界面
    //Config to load process interface
    this.loadingView = new LoadingUI();
    this.stage.addChild(this.loadingView);

    //初始化Resource资源加载库
    //initiate Resource loading library
    RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
    RES.loadConfig("resource/default.res.json", "resource/");
  }

  /**
   * 配置文件加载完成,开始预加载preload资源组。
   * configuration file loading is completed, start to pre-load the preload resource group
   */
  private onConfigComplete(event: RES.ResourceEvent): void {
    RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
    RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
    RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
    RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
    RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
    RES.loadGroup("startPage");
    RES.loadGroup("playingPage");
  }

  /**
   * preload资源组加载完成
   * Preload resource group is loaded
   */
  private onResourceLoadComplete(event: RES.ResourceEvent) {
    if (event.groupName == "startPage") {
      this.stage.removeChild(this.loadingView);
      RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
      RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
      RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
      RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
      //背景
      let sky = new egret.Bitmap();
      sky.texture = RES.getRes("game_scene_bg");
      this.addChild(sky);
      sky.width = this.stage.stageWidth;
      sky.height = this.stage.stageHeight;

      this.addChild(this.startPage);
    }
  }

  /**
   * 资源组加载出错
   *  The resource group loading failed
   */
  private onItemLoadError(event: RES.ResourceEvent) {
    console.warn("Url:" + event.resItem.url + " has failed to load");
  }

  /**
   * 资源组加载出错
   *  The resource group loading failed
   */
  private onResourceLoadError(event: RES.ResourceEvent) {
    //TODO
    console.warn("Group:" + event.groupName + " has failed to load");
    //忽略加载失败的项目
    //Ignore the loading failed projects
    this.onResourceLoadComplete(event);
  }

  /**
   * preload资源组加载进度
   * Loading process of preload resource group
   */
  private onResourceProgress(event: RES.ResourceEvent) {
    if (event.groupName == "startPage") {
      this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal);
    }
  }

  private textfield: egret.TextField;

  /**
   * 创建游戏场景
   * Create a game scene
   */

  /*private touchHandler( evt:egret.TouchEvent ):void{
    let tx:egret.TextField = evt.currentTarget;
    tx.textColor = 0x00ff00;
  }*/

  /**
   * 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
   * Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
   */


  /**
   * 描述文件加载成功，开始播放动画
   * Description file loading is successful, start to play the animation
   */
  /*private startAnimation(result: string[]) {
    let parser = new egret.HtmlTextParser();

    let textflowArr = result.map(text => parser.parse(text));
    let textfield = this.textfield;
    let count = -1;
    let change = () => {
      count++;
      if (count >= textflowArr.length) {
        count = 0;
      }
      let textFlow = textflowArr[count];

      // 切换描述内容
      // Switch to described content
      textfield.textFlow = textFlow;
      let tw = egret.Tween.get(textfield);
      tw.to({"alpha": 1}, 200);
      tw.wait(2000);
      tw.to({"alpha": 0}, 200);
      tw.call(change, this);
    };

    change();
  }*/
}


